Skip to content

fix: prevent crash when disconnecting etcd connection#456

Merged
datlechin merged 2 commits intomainfrom
fix/etcd-disconnect-crash
Mar 25, 2026
Merged

fix: prevent crash when disconnecting etcd connection#456
datlechin merged 2 commits intomainfrom
fix/etcd-disconnect-crash

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

Fixes a crash (EXC_CRASH SIGABRT) when disconnecting an etcd connection while requests are in-flight (ping, watch, or auth).

Root cause: EtcdHttpClient.disconnect() calls session.invalidateAndCancel(), but in-flight async requests had already captured the URLSession reference outside the lock. When the continuation closure later calls session.dataTask(with:) on the invalidated session, CFNetwork throws an Objective-C NSException — which is uncatchable by Swift do/catch — causing abort().

Fix: Added a sessionGeneration counter that increments on disconnect. All three dataTask creation sites (performRequest, watch, authenticate) now re-acquire the lock and verify the generation matches before calling dataTask. If the session was invalidated, they throw EtcdError.notConnected instead of crashing.

Crash sites fixed (all in EtcdHttpClient.swift):

  • performRequest — line 717 (general API calls, ping)
  • watch — line 574 (key watch long-poll)
  • authenticate — line 817 (token refresh)

Test plan

  • Connect to an etcd server
  • While connected, disconnect — verify no crash
  • While a watch is active, disconnect — verify no crash
  • Rapidly connect/disconnect — verify no crash
  • Verify normal etcd operations still work after reconnecting

@datlechin datlechin merged commit d740ca9 into main Mar 25, 2026
2 checks passed
@datlechin datlechin deleted the fix/etcd-disconnect-crash branch March 25, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant